package com.facefun.web;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.facefun.Enum.StateCode;
import com.facefun.co.ChaptorCo;
import com.facefun.domain.Admin;
import com.facefun.domain.Cartoon;
import com.facefun.domain.Chaptor;
import com.facefun.service.AdminService;
import com.facefun.service.CartoonService;
import com.facefun.service.ChaptorService;
import com.facefun.service.PicService;
import com.facefun.service.TypeService;
import com.facefun.service.UserService;
import com.facefun.vo.CartoonSearchVo;



@Controller
public class BackController{

	@Autowired
	private CartoonService cartoonService;
	
	@Autowired
	private TypeService typeService;
	
	@Autowired
	private ChaptorService chaptorService;
	
	@Autowired
	private PicService picService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private AdminService adminService;
	
	@PostMapping("/cartoon")
	public String cartoonAdd(@RequestParam(value="ctImg",required=false)MultipartFile Img,
			@RequestParam(value="ctPic",required=false)MultipartFile Pic, 
			Cartoon cartoon,HttpServletRequest request){
		cartoonService.addCartoon(Img, Pic, cartoon, request);
		return "redirect:/admin/cartoon";
	}
	
	@PutMapping("/cartoon")
	public String cartoonUpdate(@RequestParam(value="ctImg",required=false)MultipartFile Img,
			@RequestParam(value="ctPic",required=false)MultipartFile Pic,
			Cartoon cartoon,HttpServletRequest request){
		cartoonService.updateCartoon(Img, Pic, cartoon, request);
		return "redirect:/admin/cartoon";
	}
	

	@DeleteMapping("/cartoon")
	public @ResponseBody StateCode cartoonDelete(Cartoon cartoon,HttpServletRequest request){
		return cartoonService.removeCartoon(cartoon, request);
	}
	
	@PostMapping(value="/search/{id}")
	public @ResponseBody Cartoon getCartoonToUpdate(@PathVariable("id")Integer id){
	
		return cartoonService.selectByPrimaryKey(id);
	}
	

	@PostMapping("/cartoon/list")
	public @ResponseBody Map<String,Object> getCartoonList(@RequestParam(value="columns[1][search][value]",required=false)String ctName,
			@RequestParam(value="columns[2][search][value]",defaultValue="0")Integer typic,
			@RequestParam(value="columns[4][search][value]",required=false)Boolean statu,Integer start,Integer length){
  
		return  cartoonService.getCartoonList(new CartoonSearchVo(ctName, typic, statu), start, length);
	}
	
	@PostMapping("/user/list")
	public @ResponseBody Map<String,Object> getUserList(Integer start,Integer length){

		return  userService.getUserList( start, length);
	}
	
	@GetMapping("/admin/cartoon")
	public String cartoon(Model model){
		model.addAttribute("types", typeService.selectAll());
		return "back/cartoon";
		
	}
	
	@GetMapping("/admin/cartoon/editor")
	public String cartoonEditor(@RequestParam(name="id",required=false)Integer key,Model model){
		Cartoon c=new Cartoon();
		if(key!=null){
			c=cartoonService.selectByPrimaryKey(key);
		}
		model.addAttribute("types", typeService.selectAll());
		model.addAttribute("c",c);
		return "back/cartooneditor";
		
	}
	
	@GetMapping("/backlogin")
	public String backLoginPage(){
		return "back/backlogin";
	}
	
	@PostMapping("/backlogin")
	public String backLogin(Admin admin,HttpServletRequest request){
		Admin a= adminService.selectOne(admin);
		if(a!=null){
			request.getSession().setAttribute("admin",a);
			return "redirect:/admin/cartoon";
		}
		
			request.setAttribute("error", "用户名或密码错误");
			return "back/backlogin";
		
	}
	
	@GetMapping("/admin/exit")
	public String adminExit(HttpSession session){
		session.removeAttribute("admin");
		return "redirect:/backlogin";
	}
	
	@GetMapping("/admin/chaptor")
	public String chaptor(@RequestParam("ctId")Integer ctId,Model model){
		model.addAttribute("c", cartoonService.getCartoonCoByKey(ctId));
		return "back/chaptor";
		
	}
	
	@GetMapping("/admin/user")
	public String user(){
		return "back/user";
	}
	
	
	@GetMapping("/admin/upload")
	public String upload(@RequestParam("ctId")Integer ctId,
			@RequestParam(name="cpId",required=false)Integer cpId,Model model){
		if(cpId!=null){
			model.addAttribute("cp", chaptorService.getChaptorCoByKey(ctId, cpId));
		}else{
			Cartoon c=cartoonService.selectByPrimaryKey(ctId);
			ChaptorCo cp=new ChaptorCo();
			cp.setCtId(c.getId());
			cp.setCtName(c.getName());
			cp.setCpId(c.getLatestNum()+1);
			model.addAttribute("cp",cp);
		}
		return "back/upload";
	}
	
	
	@PostMapping("/chaptor")
	public String  uploadAdd(@RequestParam("pic")MultipartFile[] files,Chaptor chaptor,HttpServletRequest request){
		chaptorService.addChaptor(chaptor, files, request);
		return "redirect:/admin/chaptor?ctId="+chaptor.getCtId();
	}
	
	@PutMapping("/chaptor")
	public String chaptorPut(@RequestParam("pic")MultipartFile[] files,Chaptor chaptor,HttpServletRequest request){
		chaptorService.updateChaptor(chaptor, files, request);
		return "redirect:/admin/chaptor?ctId="+chaptor.getCtId();
	}

	@DeleteMapping("/chaptor")
	public @ResponseBody StateCode chaptorDelete(Chaptor chaptor,HttpServletRequest request){
		return chaptorService.removeChaptor(chaptor, request);
	}
	
	@PostMapping("/pic/delete")
	public @ResponseBody StateCode picDelete(Integer id,HttpServletRequest request){
	
		return picService.removePic(id, request);
	}
}
